<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*, loginbean.*" %>
<%
	ManageObjects manager = new ManageObjects();
	if(request.getParameter("Submit").trim().equals("Change Object"))
	{
		if(request.getParameter("newObjectName").length() != 0)
			manager.editObject(new Integer(request.getParameter("selectedObject")).intValue(), request.getParameter("newObjectName"));
	}
	else if(request.getParameter("Submit").trim().equals("Delete Field"))
	{
		System.out.println(request.getParameter("selectedField"));
		manager.deleteField(new Integer(request.getParameter("selectedField")).intValue());
	}
	else if(request.getParameter("Submit").trim().equals("Change Fields"))
	{
		for(Enumeration e = request.getParameterNames(); e.hasMoreElements();)
		{
			String param = (String)e.nextElement();
			if(request.getParameter(param).length() > 0)
			{
				int fieldId;
				if(param.startsWith("newTypeName"))
				{
					fieldId = new Integer(param.substring(11)).intValue();
					manager.editField(fieldId, "", request.getParameter(param));
				}
				else if(param.startsWith("newFieldName"))
				{
					fieldId = new Integer(param.substring(12)).intValue();
					manager.editField(fieldId, request.getParameter(param), "");
				}
			}
		}
		manager.closeDb();
	}
%>
<%ObjectMap objectList = new ObjectMap(new Integer(session.getAttribute("tenantid").toString()).intValue()); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Edit Object Page</title>
<SCRIPT language="JavaScript">

function deleteField( fldId )
{
	document.editFieldForm.selectedField.value=fldId;
}

</SCRIPT>
</head>
<body>

<%
	Iterator objectNames = objectList.keySet().iterator();
	while( objectNames.hasNext() )
	{
		String objectId = (String)objectNames.next();
		if(objectId.equals(request.getParameter("selectedObject")))
		{%>
			<form method="POST" name="nameChangeForm" action="">
			<table border="1">
				<tr>
				<th>Object Name</th><th>New Object Name</th></tr>
				<%		
				ObjectBean obj = (ObjectBean)objectList.get(objectId);
				String objectName = obj.getName();
				out.println("<tr><td>");
				out.println(objectName + "</td><td>");
				%>
				<input type="text" name="newObjectName" value="" /></td>
				<%
				out.print( "<td><input type=\"submit\" name=\"Submit\" value=\"Change Object\" >");
				out.println("</td></tr>");
				%>
				</tr>
			</table>
			<input type="hidden" name="selectedObject" value="<% out.print(objectId); %>">
			</form>
			
			<form method="POST" name="editFieldForm" action="">
			<input type="hidden" name="selectedField" value="">
			<table border="1"> 
			<tr>
			<th>Field Name</th><th>New Field Name</th><th>Type Name</th><th>New Type Name</th><th>Delete Field</th></tr>
			<%
				FieldBean[] fields = obj.getFields();
				for(int i=0; i<fields.length; i++)
				{
					out.println("<tr><td>");
					out.println(fields[i].getName() + "</td><td>");
					%><input type="text" name="newFieldName<%out.print(fields[i].getFieldId()); %>" value="" /></td><%
					
					out.println("<td>");
					out.println(fields[i].getDataType() + "</td><td>");
					%><input type="text" name="newTypeName<%out.print(fields[i].getFieldId()); %>" value="" /></td><%
					
					if(fields.length > 1)
					{						
						out.print( "<td><input type=\"submit\" name=\"Submit\" value=\"Delete Field\" onClick=\"deleteField('"+ fields[i].getFieldId() +"')\"></td>");
					}
					out.println("</tr>");
				}%>
				
				
			</table>
			<input type="submit" name="Submit" value="Change Fields" />
			</form> 
		<%}
	}
%>
<br><a href="welcome.jsp">Main Menu</a>
</body>
</html>